﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Sherwood.SignOn.Client
{
    /// <summary>
    /// Example implementation of a sign-in handler which parses a ticket response from the
    /// SSO Server and enables local authentication of the user.
    /// </summary>
    public class SignInHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request != null && context.Request.QueryString != null)
            {
                string ticket = context.Request.QueryString["ticket"] ?? "";
                string signature = context.Request.QueryString["signature"] ?? "";
                string returnurl = context.Request.QueryString["returnurl"] ?? "";

                SignOnClient ssoClient = new SignOnClient();
                if (ssoClient.ClientSignOn(ticket, signature))
                {
                    if (!string.IsNullOrEmpty(returnurl))
                    {
                        context.Response.Redirect(returnurl, true);
                    }
                    else
                    {
                        context.Response.Clear();
                        context.Response.End();
                    }
                }
                else
                {
                    ssoClient.RedirectToSsoServer(returnurl,true);
                }
            }
        }

        #endregion
    }
}
